package algorithm

import (
	"TODecider/src/structs"
	"TODecider/src/util"
	"math/rand"
	"time"
)

type Random struct{}

// Match By Random algorithm with O(N) time complexity
func (random *Random) Match(wbg structs.WBG) map[int]int {
	graph := wbg.Graph
	E := len(graph)
	res := make(map[int]int)
	val := 0.0
	shuffled := make([]int, E)
	iteration = E
	startTime := time.Now()
	// array initialization
	for i := 0; i < E; i++ {
		shuffled[i] = i
	}
	// generate shuffled sort by fisher-yates algorithm\
	for i := E - 1; i >= 0; i-- {
		num := rand.Intn(i + 1)
		shuffled[i], shuffled[num] = shuffled[num], shuffled[i]
		res[i] = shuffled[i]
	}
	// execution time statistics
	duration := int(time.Since(startTime) / 1e6)
	// clean the result
	for i := 0; i < E; i++ {
		val -= graph[i][res[i]]
	}
	util.PrintTOEvaluation(res, val, iteration, duration)
	return res
}
